package com.atgouwu.mall.order.constant;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atgouwu.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 支付宝
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    public String app_id = "9021000135629853"; //应用id(收款账号是APPID对应支付宝账号),控制台拷入https://open.alipay.com/develop/manage,测试用沙箱appid,控制台->(下面)开发工具推荐(沙箱)

    //商户私钥(PKCS8格式RSA2私钥),即支付宝密钥生成器(应用私钥)
    public String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCx8JGh7K5D6pZkStyAR4cLfNFF6CTPg0OXEafDLPggZl+BCQo63LhLXF6as/rke0vEtZsqfc5LFvrTwthuoY2j7/yQ7+4NCJkbxcf155jvgeRHE194ZEHhSTn7EQqh3IlWWC2dLu00e3P8Rg5BX2x880HFIkETlSD7JNun5dL00i8Vvj5pcXN6uwauHwbwQvv6pCEkRopqFEGSV1P/OSuJg3p1+vvGdeS4NcZFMe/HjlZAArEAgaKSqyl+PZhyILqtkZJUa5MqwLYIu6nebaeQIk1/uSP96BOn+OXFuYl+HnZvRR+SJ3l03lNb7KI7LqEUuNfOmsfvLRU5bWCYzkpFAgMBAAECggEAbxEVJfhA8h2cT++2YM/EyHPZgIQdbfWmG8ama5pHKpob5MQKwaEB2/BFENVktncLXTZqKwSp3o7yc3l8dNmcOobSpHlOoo4KW56BaMlt2Zm/Lmo2PkR+Jqv5Z/BELbIle2LA3grrfsXMKNxli4KPihKgq/bsndeI5ZolDVDT84Gu6pFbuHPu70x883UZtiiXdDNXB76efXvxNH4rsubc3z6KbH4G7+Gr0vCEx+xZfqUy9tpTGcXVHU8xCnW+rg9lUeVeRvryNOEi75T5yJdZ56YsDC1N7R9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gkb9BsBAj+EoCT73jx7fQvRpEiJLWFiPI13mMdA2Z/kiYc4TzbyjwMfndysVZhNdhmqQouFXGg2ncyUePvUZGlJL4lmllN6BU8IYkNZhOQg7l+VBUAjt6pO82Pu30TEpD4e4jMRl12GfKgsX5/33Ew7KseWvOUrKSpILRs08MG/xCh33kBDVuy54pkb08ykG/WLnB9byzQWbeWW+mkRC1/7Nt3dV4me8HnMmCC3S74CupKWHlvyeDbm/Vf+1FVkN2Xvq1N92ewNN+UJdWuyHv0O/9yiVwIDAQAB";

    public String notify_url = "http://hotlmall.cn/order/paymentinfo/payed/notify"; //异步回调后端接口,必须外网可访(或内网穿透),分布式事务(最大努力通知),(需http://格式完整路径,不能加?id=1自定义参数,必须外网可访问,或内网穿透)

    public String return_url = "http://localhost:8080/#/PaySuccess"; //同步回调前端页面,即跳转到成功或失败页

    private String sign_type = "RSA2"; //签名加密方式

    private String charset = "utf-8"; //字符编码格式

    private String timeout = "10m"; //订单超时时间

    public String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do"; //支付宝网关地址(沙箱或自建应用中)

    //public static String log_path = "E:\\"; //日志

    //支付
    public String pay(PayVo vo) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type); //支付客户端
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); //支付请求
        alipayRequest.setReturnUrl(return_url); //请求参数
        alipayRequest.setNotifyUrl(notify_url);
        String out_trade_no = vo.getOut_trade_no(); //订单号,必填
        String total_amount = vo.getTotal_amount(); //付款金额,必填
        String subject = vo.getSubject(); //订单名称,必填
        String body = vo.getBody(); //商品描述,可空
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String result = alipayClient.pageExecute(alipayRequest).getBody(); //响应一个页面,浏览器显示这个页面,自动到支付宝收银台页面
        //System.out.println("支付宝的响应："+result);
        return result;
    }
}
